{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# CPW media example\n",
    "\n",
    "A coplanar waveguide (CPW) is a planar transmission line where the return path is a plane located on both sides of a strip conductor on the same layer. The characteristic impedance depends on the strip width and the gap with the return plane, giving two degrees of freedom to the designer.\n",
    "\n",
    "A great advantage of CPW is the convenience to perform shunts or series connections with components because both the signal and the return path are on the same side, thus no via is required. On the other hand, the large coplanar planes require a lot of area.\n",
    "\n",
    "The coplanar waveguide has quasi-TEM mode and less dispersion than microstrip. To avoid the propagation of parasitic slotline mode, the planes on both sides of the strip have to be connected together. This could be done using air bridges on MMICs or by adding a conductor plane on the backside of the substrate and connecting all planes together with via fences. \n",
    "\n",
    "When using conductor backing, the line could behave like a microstrip line instead, should the height of the substrate be small and the gap with coplanar planes be big.\n",
    "\n",
    "![isometric view of coplanar waveguide structure without conductor backing](coplanar_waveguide_structure.svg \"coplanar waveguide structure without conductor backing\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "import skrf as rf\n",
    "from skrf.calibration import IEEEP370_SE_NZC_2xThru\n",
    "from skrf.media import CPW\n",
    "\n",
    "rf.stylely()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Measurement of two CPWG lines with different lengths\n",
    "\n",
    "The measurement was performed on the 21st of March 2017 on an Anritsu MS46524B 20GHz Vector Network Analyser. The setup is a linear frequency sweep from 1 MHz to 10 GHz with 10'000 points. Output power is 0 dBm, IF bandwidth is 1kHz and neither averaging nor smoothing were used.\n",
    "\n",
    "CPWGxxx is an L long, W wide, with a G wide gap to top ground, T thick copper coplanar waveguide with conductor backing on an H height substrate with top and bottom ground plane. A closely spaced vias wall is placed on both sides of the line and the top and bottom ground planes are connected by many vias.\n",
    "\n",
    "| Name | L (mm) | W (mm) | G (mm) | H (mm) | T (um) | Substrate |\n",
    "| :--- | ---: | ---: | ---: | ---: | ---: | :--- |\n",
    "| CPW100 | 100 | 1.70 | 0.50 | 1.55 | 50 | FR-4 |\n",
    "| CPW200 | 200 | 1.70 | 0.50 | 1.55 | 50 | FR-4 |\n",
    "\n",
    "The milling of the artwork is performed mechanically with a lateral wall of 45°. The relative permittivity of the dielectric was assumed to be approximately 4.5 for design purposes.\n",
    "\n",
    "![MSL100 and MSL200 illustration, both are microstripline, MSL200 is twice the length of MSL100](MSL_CPWG_100_200.jpg \"MSL100 and MSL200\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Looking at measurements\n",
    "\n",
    "We look at measurement insertion loss phase and magnitude as well as return loss phase and magnitude.\n",
    "\n",
    "The insertion loss of CPWG200 is twice that of CPWG100 and is mostly linear up to 6 GHz. The phase shows also a factor two. Both statements agree with the fact that CPWG200 has twice the length of CPWG100.\n",
    "\n",
    "The return loss insertion and phase have the same order of magnitude for the two networks. Insertion loss is below -20 dB at low frequency which indicates a good match of the impedance. The distance between magnitude minima is two times shorter with CPWG200 related to CPWG100, which again agrees with the physical length difference."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load raw measurements\n",
    "TL100 = rf.Network('CPWG100.s2p')\n",
    "TL200 = rf.Network('CPWG200.s2p')\n",
    "\n",
    "# plot them all\n",
    "plt.figure(figsize=(10, 10))\n",
    "plt.suptitle('Raw measurements')\n",
    "plt.subplot(2, 2, 1)\n",
    "TL100.plot_s_db(0, 0)\n",
    "TL200.plot_s_db(0, 0)\n",
    "TL100.plot_s_db(1, 1)\n",
    "TL200.plot_s_db(1, 1)\n",
    "plt.subplot(2, 2, 2)\n",
    "TL100.plot_s_deg(0, 0)\n",
    "TL200.plot_s_deg(0, 0)\n",
    "TL100.plot_s_deg(1, 1)\n",
    "TL200.plot_s_deg(1, 1)\n",
    "plt.subplot(2, 2, 3)\n",
    "TL100.plot_s_db(1, 0)\n",
    "TL200.plot_s_db(1, 0)\n",
    "TL100.plot_s_db(0, 1)\n",
    "TL200.plot_s_db(0, 1)\n",
    "plt.subplot(2, 2, 4)\n",
    "TL100.plot_s_deg(1, 0)\n",
    "TL200.plot_s_deg(1, 0)\n",
    "TL100.plot_s_deg(0, 1)\n",
    "TL200.plot_s_deg(0, 1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Removing connectors effects\n",
    "\n",
    "The SMA connectors cause a discontinuity in the signal path. The waves have to go through a coaxial to planar transition with a complex three-dimensional geometry in the launch region.\n",
    "\n",
    "By splitting the 100 mm line into two halves and deembed them from the 200 mm line, the connector and 50 mm of the line will be removed on both sides. This will remove the effects caused by the connectors."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# deembedding using IEEEP370 impedance corrected 2xthru method\n",
    "dm = IEEEP370_SE_NZC_2xThru(dummy_2xthru = TL100, name = '2xthru')\n",
    "fix1 = dm.s_side1\n",
    "fix1.name = 'FIX-1'\n",
    "fix2 = dm.s_side2\n",
    "fix2.name = 'FIX-2'\n",
    "d_dut = dm.deembed(TL200)\n",
    "d_dut.name = 'd_DUT'\n",
    "\n",
    "# plot them all\n",
    "plt.figure(figsize=(10, 10))\n",
    "plt.suptitle('Connectors models')\n",
    "plt.subplot(2, 2, 1)\n",
    "fix1.plot_s_db(0, 0)\n",
    "fix2.plot_s_db(0, 0)\n",
    "plt.subplot(2, 2, 2)\n",
    "fix1.plot_s_deg(0, 0)\n",
    "fix2.plot_s_deg(0, 0)\n",
    "plt.subplot(2, 2, 3)\n",
    "fix1.plot_s_db(1, 0)\n",
    "fix2.plot_s_db(1, 0)\n",
    "plt.subplot(2, 2, 4)\n",
    "fix1.plot_s_deg(1, 0)\n",
    "fix2.plot_s_deg(1, 0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Comparison of deembedded dut with CPW media results\n",
    "We now compare the results of our deembedded measurements with simulated results from CPW media.\n",
    "\n",
    "The ep_r and tanD values are taken from the \"Correlating microstripline model to measurement\" notebook in the same directory.\n",
    "\n",
    "The insertion loss agreement is very good both in phase and magnitude.\n",
    "\n",
    "The return loss overall magnitude seems correct and indicates a good match. Return losses below -20 dB are tricky to measure because the signal becomes small. Thus we should not expect to have a good agreement on the shape here."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "ep_r = 4.421\n",
    "tanD = 0.0167\n",
    "\n",
    "cpw = CPW(frequency=d_dut.frequency, w = 1.7e-3, s = 0.5e-3, t = 50e-6, h = 1.55e-3,\n",
    "        ep_r = ep_r, tand = tanD, rho = 1.7e-8, z0_port = 50., has_metal_backside = True)\n",
    "l = cpw.line(d = 100.0e-3, unit = 'm')\n",
    "l.name = 'model'\n",
    "\n",
    "# plot them all\n",
    "plt.figure(figsize=(10, 10))\n",
    "plt.suptitle('Comparison deembedded measurement and simulation')\n",
    "plt.subplot(2,2,1)\n",
    "d_dut.plot_s_db(0,0)\n",
    "l.plot_s_db(0,0,)\n",
    "plt.subplot(2,2,2)\n",
    "d_dut.plot_s_deg(0,0)\n",
    "l.plot_s_deg(0,0)\n",
    "plt.subplot(2,2,3)\n",
    "d_dut.plot_s_db(1,0)\n",
    "l.plot_s_db(1,0)\n",
    "plt.subplot(2,2,4)\n",
    "d_dut.plot_s_deg(1,0)\n",
    "l.plot_s_deg(1,0)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Checking deembedding\n",
    "Two basic checks can be done to ensure the consistency of the model of the sides generated by IEEEP370 2xthru algorithm.\n",
    "\n",
    "1. Plot time step response of 2xthru, side1, and side2. The sides should look like the two halves of 2xthru. This is the case here. Plot time step response of fixture-dut-fixture it should have the same left and right shape than 2xthru. Here we can see a small impedance mismatch which will cause a bounce in dut shape. Plot dut and see if it looks like the middle portion of fixture-dut-fixture. Here everything seems OK outside the small bounce left and right of dut.\n",
    "\n",
    "1. Looks at the residuals by deembedding two sides models from 2xthru. Residual insertion magnitude shall be ± 0.1 dB and residual insertion loss phase shall be within ± 1°. This is the case for both here on the full bandwidth."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# compute residuals\n",
    "res = dm.deembed(TL100)\n",
    "res.name = 'residuals'\n",
    "res.s += 1e-15 # avoid numeric singularities\n",
    "\n",
    "# extrapolate to dc for time step\n",
    "TL100_dc = TL100.extrapolate_to_dc(kind='linear')\n",
    "TL200_dc = TL200.extrapolate_to_dc(kind='linear')\n",
    "fix1_dc = fix1.extrapolate_to_dc(kind='cubic')\n",
    "fix2_dc = fix2.extrapolate_to_dc(kind='cubic')\n",
    "d_dut_dc = d_dut.extrapolate_to_dc(kind='cubic')\n",
    "\n",
    "# plot them all\n",
    "# time domain\n",
    "plt.figure(figsize=(8, 4))\n",
    "plt.suptitle('Time domain reflexion step response (DC extrapolation)')\n",
    "TL100_dc.plot_z_time_step(0, 0)\n",
    "TL200_dc.plot_z_time_step(0, 0)\n",
    "fix1_dc.plot_z_time_step(0, 0)\n",
    "fix2_dc.plot_z_time_step(0, 0)\n",
    "d_dut_dc.plot_z_time_step(0, 0)\n",
    "plt.xlim(-2, 4)\n",
    "\n",
    "# residuals frequency domain\n",
    "plt.figure(figsize=(8, 4))\n",
    "plt.subplot(1, 2, 1)\n",
    "res.plot_s_db(1,0)\n",
    "plt.subplot(1, 2, 2)\n",
    "res.plot_s_deg(1,0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  },
  "vscode": {
   "interpreter": {
    "hash": "637c323cf467337602e9974a89cb4d3fc95fac3ef875a73e62754f8e768d8de7"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
